<template>
	<view class="update-info">
		<view class="form">
			<view class="item">
				<view class="label">手机：</view>
				<view style="color: #555;" class="value">{{ userInfo.userName }}</view>
			</view>
			<view class="item">
				<view class="label">昵称：</view>
				<input type="text" v-model="userInfo.nickName" class="value" placeholder="请输入昵称" />
			</view>
			<view class="item">
				<view class="label">性别：</view>
				<!-- <input type="text" v-model="userInfo.sex" class="value" /> -->
				<picker :range="sexArr" class="value" mode="selector" :value="userInfo.sex" @change="sexChange">
					{{ sexEnum[userInfo.sex] }}</picker>
			</view>
		</view>
		<div class="btns">
			<button class="btn" @click="submit">确认修改</button>
		</div>
	</view>
</template>

<script>
	import {
		updateUserInfo,
		getUserInfo
	} from "../../api/user.js"
	import {
		sexEnum
	} from "../../enum/user.js"
	export default {
		data() {
			return {
				userInfo: {
					nickName: "",
					userName: "",
					sex: 0,
					userType: null,
					id: null,
				},
				sexArr: ["女", "男", "未知"],
				sexEnum,
			};
		},
		created() {
			this.userInfo.id = uni.getStorageSync("userInfo").userId
			this.getUserInfos()
		},
		methods: {
			async getUserInfos() {
				const res = await getUserInfo({
					id: this.userInfo.id
				})
				console.log(res)
				if (res.code === 200) {
					this.userInfo.nickName = res.data.nickName
					this.userInfo.sex = res.data.sex
					this.userInfo.userName = res.data.userName
					this.userInfo.userType = res.data.userType
					return
				}

				uni.showToast({
					title: res.msg,
					icon: "error"
				})
			},
			sexChange(e) {
				const val = e.detail.value;
				this.userInfo.sex = val;
			},
			async submit() {
				if (!this.userInfo.nickName || !this.userInfo.sex) {
					uni.showToast({
						title: "表单不完整",
						icon: "error"
					})
					return
				}

				const res = await updateUserInfo({
					...this.userInfo,
				})

				if (res.code === 200) {
					uni.setStorageSync("userInfo", this.userInfo)
					uni.showToast({
						title: "修改成功",
						icon: "success"
					})
					return
				}

				uni.showToast({
					title: res.msg,
					icon: "error"
				})
			}
		}
	}
</script>

<style lang="scss">
	.update-info {
		width: 100%;
		height: 100vh;
		background-color: #F7F8FA;
		box-sizing: border-box;
		padding: 40rpx;

		.btns {
			width: 100%;
			display: flex;
			justify-content: center;
			margin-top: 100rpx;

			.btn {
				background-color: #1989FA;
				width: 100%;
				color: #fff;
			}
		}

		.form {
			width: 100%;
			background-color: #fff;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 20rpx;


			.item {
				height: 80rpx;
				width: 100%;
				display: flex;
				align-items: center;
				border-bottom: 1px solid #eee;

				&:last-of-type {
					border: none;
				}

				.label {
					margin-right: 20rpx;
				}

				.value {
					height: 100%;
					flex: 1;
					line-height: 80rpx;
				}
			}
		}
	}
</style>